See Also

Tcp Class  | Tcp Members  | Overload List

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Language

Visual Basic

C#

C++

C++/CLI

Show All

buffer
Destination memory location to store received data.
See Also Languages PowerTCP SSL Sockets for .NET

BeginReceive(Byte[]) Method

Dart.PowerTCP.SslSockets Namespace > Tcp Class > BeginReceive Method : BeginReceive(Byte[]) Method

Receive data into your buffer asynchronously. The EndReceive event is raised when completed.

[Visual Basic]
<DescriptionAttribute("Receive data into your buffer asynchronously. The EndReceive event is raised when completed.")> Overloads Public Function BeginReceive( _    ByVal buffer() As Byte _ ) As IAsyncResult
[C#]
[DescriptionAttribute("Receive data into your buffer asynchronously. The EndReceive event is raised when completed.")] public IAsyncResult BeginReceive(    byte[] buffer );
[C++]
[DescriptionAttribute("Receive data into your buffer asynchronously. The EndReceive event is raised when completed.")] public: IAsyncResult* BeginReceive(    byte[]* buffer )
[C++/CLI]
[DescriptionAttribute("Receive data into your buffer asynchronously. The EndReceive event is raised when completed.")] public: IAsyncResult^ BeginReceive(    bytearray<buffer>^ buffer )

Parameters

buffer
Destination memory location to store received data.

Return Type

An IAsyncResult that represents the asynchronous operation, which could still be pending.

Exceptions

ExceptionDescription
InvalidOperationExceptionBeginXXX method used without providing an EndXXX event handler.
IOExceptionThe stream is not Readable.
ArgumentNullExceptionbuffer is null.
ArgumentOutOfRangeExceptionoffset or count is less than 0.
ArgumentExceptionoffset + count is greater than the length of buffer.
SocketExceptionThe socket is not connected.

Remarks

The BeginReceive method is used to receive data into a buffer asynchronously. When data has been received, the EndReceive event is raised. A SegmentEventArgs object is passed into this event, containing a Segment object which encapsulates information about the data itself and the amount of bytes of data received.

The object state is useful for any kind of state information you would like accessible in the EndReceive event. Since the method is "disconnected" from the calling code and returns to the calling thread elsewhere, you may wish to pass an object/variable as state to be retrieved and used in the event handler.

If you are using the Tcp component as a reference, you must "wire up" the event yourself. This involves creating a method as the event handler that implements the SegmentEventHandler delegate.

To synchronously receive data, use the Receive method.

Use this method if you wish to receive data into a buffer transparently with minimal application impact, as execution occurs on another thread. This method is the standard means for asynchronously receiving data. All available data is received.

Example

The following example demonstrates asynchronous receiving and sending of data.

[Visual Basic] 

Private Sub AsynchTcpDemo()

   ' Attempt to connect to an echo port.
   Try
      Tcp1.Connect("myserver", 7)
   Catch ex As Exception
      Return
   End Try

   ' Send data, since we are connected to an echo port, the same data should be returned.
   ' The following code demonstrates asynchronously sending data. When data has been sent
   ' the EndSend event will be raised.

   Dim buffer() As Byte = System.Text.Encoding.Default.GetBytes("a")
   Tcp1.BeginSend(buffer, 0, buffer.Length, Net.Sockets.SocketFlags.None, Nothing)
End Sub

Private Sub Tcp1_EndSend(ByVal sender As Object, ByVal e As Dart.PowerTCP.SegmentEventArgs) Handles Tcp1.EndSend
   ' Check for exception
   If e.Exception Is Nothing Then

      ' Send is complete. Display info about the data sent.
      Debug.WriteLine("Byte count sent: " + e.Segment.Count)
      Debug.WriteLine("Data sent: " + e.Segment.ToString())

      Dim buffer(Tcp1.ReceiveBufferSize) As Byte

      ' Receive the data. The EndReceive event will be raised upon completion.
      Tcp1.BeginReceive(buffer, 0, buffer.Length, Net.Sockets.SocketFlags.None, Nothing)

    End If
End Sub

Private Sub Tcp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SegmentEventArgs) Handles Tcp1.EndReceive
   ' Check for exception
   If e.Exception Is Nothing Then

      ' Receive is complete. Display info about the data sent.
      Debug.WriteLine("Byte count received: " + e.Segment.Count)
      Debug.WriteLine("Data received: " + e.Segment.ToString())

      ' Close the connection
      Tcp1.Close()
    End If
End Sub

[C#] 


private void AsynchTcpDemo()
{
   
// Attempt to connect to an echo port.
   
try
   {
       
tcp1.Connect("myserver", 7);
   }
   
catch(Exception ex)
   {
return;}

   
// Send data, since we are connected to an echo port, the same data should be returned.
   
// The following code demonstrates asynchronously sending data. When data has been sent
   
// the EndSend event will be raised.

   
byte[] buffer = System.Text.Encoding.Default.GetBytes("a");
   tcp1.BeginSend(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None,
null);
}

private void tcp1_EndSend(object sender, Dart.PowerTCP.SegmentEventArgs e)
{
   
// Check for exception
   
if(e.Exception == null)
   {
       
// Send is complete. Display info about the data sent.
       
Debug.WriteLine("Byte count sent: " + e.Segment.Count);
       Debug.WriteLine(
"Data sent: " + e.Segment.ToString());
               

       
byte[] buffer = new byte[tcp1.ReceiveBufferSize];
                   
       
// Receive the data. The EndReceive event will be raised upon completion.
       
tcp1.BeginReceive(buffer, 0, buffer.Length, System.Net.Sockets.SocketFlags.None, null);
   }
}

private void tcp1_EndReceive(object sender, Dart.PowerTCP.SegmentEventArgs e)
{
   
// Check for exception
   
if(e.Exception == null)
   {
       
// Receive is complete. Display info about the data sent.
       
Debug.WriteLine("Byte count received: " + e.Segment.Count);
       Debug.WriteLine(
"Data received: " + e.Segment.ToString());

       
// Close the connection
       
tcp1.Close();
   }
}
                

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Tcp Class  | Tcp Members  | Overload List


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.